package com.sq.partner.manager.controller;
import com.sq.partner.manager.mybatis.model.ClaimDemage;
import com.sq.partner.manager.service.AntPolicyService;
import com.sq.partner.manager.service.ClaimDemageService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
    /**
     * 蚂蚁理赔查询
     * SQ
     */
@Controller
@RequestMapping("antClaimDemageController")
public class AntClaimDemageController {
    @Resource
    ClaimDemageService claimDemageService;
    @Resource
	AntPolicyService antPolicyService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "transManager/antClaimDemage";
    }

    @RequestMapping(value = "/queryClaimDemageInfo",method = RequestMethod.POST)
    @ResponseBody
    public Page queryClaimDemageByMap(Integer pageIndex, String reportNo,
                                      String relaReportNo, String policyNo, String relaPolicyNo, String tradeBeginTime,
									  String tradeEndTime, String state, String mechan) throws Exception{
		Page page = checkBeforeQuery(pageIndex,reportNo,relaReportNo,policyNo,relaPolicyNo,tradeBeginTime,tradeEndTime,state, mechan);
        return  claimDemageService.queryClaimDemageByCondition(page);
    }
    public Page checkBeforeQuery(Integer pageIndex,String reportNo,
                                  String relaReportNo,String policyNo,String relaPolicyNo,String tradeBeginTime,
                                  String tradeEndTime,String state,  String mechan) throws Exception{
        Page page = new Page();
        Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> queryMap = new HashMap<String, Object>();
        if(pageIndex != -1){
            page.setCurrent(pageIndex);
            map = SqlUtil.buildPageMap(page);
        }
        map.put("state",state);
        map.put("report_no",reportNo);
        map.put("rela_report_no",relaReportNo);
        map.put("policy_no",policyNo);
        map.put("rela_policy_no",relaPolicyNo);
		map.put("mechan", mechan);
		queryMap.putAll(map);
		queryMap.put("tradeBeginTime",tradeBeginTime);
		queryMap.put("tradeEndTime",tradeEndTime);
        //交易日期
        if(StrUtil.isNotEmpty(tradeBeginTime)){
            tradeBeginTime = tradeBeginTime + "00:00:00";
        }
        if(StrUtil.isNotEmpty(tradeEndTime)){
            tradeEndTime = tradeEndTime + "23:59:59";
        }
        map.put("tradeBeginTime",tradeBeginTime);
        map.put("tradeEndTime",tradeEndTime);
		page.setMap(queryMap);
        page.setType(map);
        return page;
    }

	@RequestMapping(value = "exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletResponse response, HttpServletRequest request, Integer pageIndex, String report_no,
							String rela_report_no, String policy_no, String rela_policy_no, String tradeBeginTime,
							String tradeEndTime, String state, String isExc,  String mechan) throws Exception {
		pageIndex = pageIndex == null ? 1 : pageIndex;
		Page page = checkBeforeQuery(pageIndex,report_no,rela_report_no,policy_no,rela_policy_no,tradeBeginTime,tradeEndTime,state, mechan);
		//List<ClaimDemage> list = claimDemageService.queryClaimDemageByCondition(page).getData();
		List<ClaimDemage> list = claimDemageService.queryClaimDemageAll(page).getData();
		//checkDate = checkDate == null ? "" : checkDate;
		//List<AdjSoaResult> adjSoaResultList = adjSoaResultDao.queryRecCheckList(current, size, checkDate, checkType,isExc).getData();
		String fileName = "蚂蚁理赔信息表";
		String sheetName = "蚂蚁理赔信息";
		// 列数
		int columnNumber = 9;
		// 列宽
		int[] columnWidth = {5, 20, 30, 30, 15, 25, 25, 25, 25};
		// 列名
		String[] columnName = {"序号", "理赔状态", "小赔案号", "大赔案号", "理赔金额", "小保单号", "大保单号", "交易时间", "出单机构"};
		// 序号
		int count = 1;
		List<String[]> tempList = new ArrayList<String[]>();
		for (ClaimDemage c : list) {
			tempList.add(new String[]{
					String.valueOf(count++),c.getState(),c.getReportNo(),c.getRelaReportNo(),String.valueOf(c.getClaimAmount()),
					c.getPolicyNo(),c.getRelaPolicyNo(),c.getTradeDate(),c.getMechan()
			});
		}
		String[][] haidaInfos = tempList.toArray(new String[][]{});
		HSSFWorkbook wb = antPolicyService.generateHssWorkBook(sheetName, columnNumber, columnWidth, columnName, haidaInfos);
		response.setContentType("application/ms-excel;charset=utf-8");
		String userAgent = request.getHeader("user-agent").toLowerCase();
		if (userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge")) {
			// IE浏览器
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} else {
			// 非IE浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
		}
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		OutputStream os = response.getOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			os.close();
		}
	}
}
